package com.eps.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

/**
 * Utility class for IO operation
 * 
 * @author Ali Irawan
 * @version 1.0
 */
public class IOUtil {

	/**
	 * Read file and output to a stream
	 * @param file file to read
	 * @param out output stream
	 * @throws IOException
	 */
	public static void fileToStream(File file, OutputStream out) throws IOException {

		FileInputStream f = new FileInputStream(file);
		byte[] buff = new byte[256];
		int read = -1;

		while (-1 != (read = f.read(buff, 0, 256))) {
			out.write(buff, 0, read);
		}
		out.flush();
		out.close();
	}

	/**
	 * Write string to file
	 * @param file
	 * @param content
	 * @throws FileNotFoundException
	 */
	public static void writeToFile(File file, String content)
			throws FileNotFoundException {

		PrintWriter out = new PrintWriter(file);
		out.print(content);
		out.close();

	}
}
